﻿using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using myBoard.App.MagnetStore;
using myBoard.Framework;
using myBoard.App.Components.MagnetsStore;
using myBoard.App.Model;
using myBoard.App.Components.MagnetsStore.Controllers;

namespace myBoard.App
{
	public partial class AuthenticationControl
	{
        public delegate void OnRegisterRequested();
        public delegate void OnUserSignedIn(User user);
        public delegate void OnUserSigningIn();
        public delegate void OnAuthentificationFailed();
        public event OnRegisterRequested RegisterRequested;
        public event OnUserSigningIn UserSigningIn;
        public event OnUserSignedIn UserSignedIn;
        public event OnAuthentificationFailed AuthentificationFailed;
        Storyboard _showSB;
        Storyboard _hideSB;

        public AuthenticationControl()
		{
			this.InitializeComponent();
            _userNameTbx.GotFocus += new RoutedEventHandler(_userNameTbx_GotFocus);
            _userPasswordTbx.GotFocus += new RoutedEventHandler(_userPasswordTbx_GotFocus);
            _userNameTbx.KeyUp += new System.Windows.Input.KeyEventHandler(_userNameTbx_KeyUp);
            _userPasswordTbx.KeyUp += new System.Windows.Input.KeyEventHandler(_userPasswordTbx_KeyUp);
            _registerBt.Click += new RoutedEventHandler(_registerBt_Click);
            _signInBt.Click += new RoutedEventHandler(_signInBt_Click);
            _showSB = this.Resources["ShowSB"] as Storyboard;
            _hideSB = this.Resources["HideSB"] as Storyboard;
		}

        public void CustomShow()
        {
            this.BeginStoryboard(_showSB);
        }

        public void CustomHide()
        {
            this.BeginStoryboard(_hideSB);
        }

        public void ConnectToStore()
        {
            MagnetsStoreClient.Client.SignInCompleted += new EventHandler<SignInCompletedEventArgs>(_magnetStoreClient_SignInCompleted);
        }

        void _userPasswordTbx_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                _signInBt_Click(sender, e);
            }
        }

        void _userNameTbx_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                _signInBt_Click(sender, e);
            }
        }

        void _magnetStoreClient_SignInCompleted(object sender, SignInCompletedEventArgs e)
        {
            StoreController.UpdateStatus("");            
            if (e.Error!=null)
            {
                ExceptionsManager.LogException("Magnet Store", e.Error, false);
                _authentificationFailedText.Visibility = Visibility.Visible;
                if (AuthentificationFailed != null)
                    AuthentificationFailed();
                return;
            }
            else
            {
                _authentificationFailedText.Visibility = Visibility.Hidden;
                User user = MagnetsStoreClient.Client.GetConnectedUserInfo(e.Result);
                
                Instances instances = new Instances();
                instances.UsersViewModel.ConnectedUser = user;
                instances.UsersViewModel.ConnectedUserSessionId = e.Result;
                instances.MagnetsViewModel.RaisePropertyChanged("ConnectedUserSubmittedMagnets");
                instances.MagnetsViewModel.RaisePropertyChanged("FeaturedMagnet");

                if (UserSignedIn != null)
                    UserSignedIn(user);
            }
        }

        void _signInBt_Click(object sender, RoutedEventArgs e)
        {
            if (UserSigningIn != null)
                UserSigningIn();            
            MagnetsStoreClient.Client.SignInAsync(_userNameTbx.Text, _userPasswordTbx.Password);
        }

        void _registerBt_Click(object sender, RoutedEventArgs e)
        {
            if (RegisterRequested != null)
            {
                RegisterRequested();
            }
        }

        void _userPasswordTbx_GotFocus(object sender, RoutedEventArgs e)
        {
            _userPasswordTbx.Password = string.Empty;
        }

        void _userNameTbx_GotFocus(object sender, RoutedEventArgs e)
        {
            _userNameTbx.Text = string.Empty;
        }
	}
}